package cn.school.teacher.evaluation.service.impl;

import cn.school.common.exception.ApiException;
import cn.school.teacher.evaluation.dao.TeacherEvaluationGradeDao;
import cn.school.teacher.evaluation.dao.TeacherEvaluationItemDao;
import cn.school.teacher.evaluation.entity.TeacherEvaluationGrade;
import cn.school.teacher.evaluation.service.TeacherEvaluationGradeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherEvaluationGradeServiceImpl extends ServiceImpl<TeacherEvaluationGradeDao, TeacherEvaluationGrade> implements TeacherEvaluationGradeService {
    @Resource
    private TeacherEvaluationItemDao teacherEvaluationItemDao;

    @Override
    public List<TeacherEvaluationGrade> listScheId(Long scheId) {
        int sumScore = this.teacherEvaluationItemDao.sumScoreCheck(scheId);
        LambdaQueryWrapper<TeacherEvaluationGrade> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeacherEvaluationGrade::getTescId, scheId);
        queryWrapper.orderByAsc(TeacherEvaluationGrade::getTegrLevelCode);
        List<TeacherEvaluationGrade> list = this.list(queryWrapper);
        if (list != null && list.size() != 0) {
            int size = list.size();
            int index = 0;
            for (TeacherEvaluationGrade teacherEvaluationGrade : list) {
                if (index > size-1) {
                    break;
                }
                if (index == 0) {
                    teacherEvaluationGrade.setBeginScoreArea(teacherEvaluationGrade.getTegrLevelScore());
                    teacherEvaluationGrade.setEndScoreArea(sumScore);
                }else if (index > 0 && (index < size)) {
                    teacherEvaluationGrade.setBeginScoreArea(teacherEvaluationGrade.getTegrLevelScore());
                    teacherEvaluationGrade.setEndScoreArea(list.get(index-1).getTegrLevelScore());
                }
                index ++;
            }
        }
        return list;
    }

    @Override
    public void saveOrUpdateBiz(TeacherEvaluationGrade teacherEvaluationGrade) {
        List<TeacherEvaluationGrade> dbList = this.listScheId(teacherEvaluationGrade.getTescId());

        // ===== 校验条件计算 ========
        // 考评项的总分
        int sumScore = this.teacherEvaluationItemDao.sumScoreCheck(teacherEvaluationGrade.getTescId());

        if (sumScore == 0) {
            throw new ApiException("请先设置考评项百分比分数（根据分数划等级）");
        }

        // 最后等级的分数
        TeacherEvaluationGrade lastScoreItem = null;
        if (dbList != null && dbList.size() != 0) {
            //
            lastScoreItem = dbList.get(dbList.size() - 1);
        }
        // ===== 校验===== ========
        if (lastScoreItem != null) {
            if (teacherEvaluationGrade.getTegrLevelScore() >= lastScoreItem.getTegrLevelScore() || teacherEvaluationGrade.getTegrLevelScore() < 0) {
                throw new ApiException("等级分数必须在[" + 0 + " ~ " + lastScoreItem.getTegrLevelScore() + ")区间");
            }
        } else {
            if (teacherEvaluationGrade.getTegrLevelScore() > sumScore || teacherEvaluationGrade.getTegrLevelScore() < 0) {
                throw new ApiException("等级分数必须在[" + 0 + " ~ " + sumScore + "]区间");
            }
        }
        teacherEvaluationGrade.setTegrLevelCode(dbList.size() + 1);
        this.save(teacherEvaluationGrade);
    }
}
